package cn.edu.nuc.ssm.controller.film;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.edu.nuc.ssm.controller.page.Pager;
import cn.edu.nuc.ssm.domain.Type;
import cn.edu.nuc.ssm.service.interfaces.TypeService;

@Controller
@RequestMapping("type")
public class TypeController {
    @Autowired
	private TypeService service;
    
    /**
	 * 通过servlet进入添加影片类型页面。
	 * @return
	 */
    @RequestMapping(value="add",method=RequestMethod.GET)
    public String getAdd(){
    	return "film/type/add";
    }
    /**
     * 添加影片类型（post）
     * @param type
     * @return
     */
    @RequestMapping(value="add",method=RequestMethod.POST)
    public String addType(Type type){
    	int result = service.insert(type);
    	if(result == 1){
    		return "redirect:list";
    	}
    	return "error";
    } 
	/**
	 * 进入影片类型列表(查询)
	 * @return
	 */
	@RequestMapping(value="list",method=RequestMethod.GET)
	public String getFun(@RequestParam(name="typename",defaultValue="") String typename,
			@RequestParam(name="current",defaultValue="1") int current , 
			Model model){
		Pager<Type> pager = service.pageList(typename, current);
		model.addAttribute("pager", pager);
		return "film/type/list";
	}
	
	/**
	 * 更新，（全部进行更新）
	 */
	//获取影片类型信息
	@RequestMapping(value="detail/{typeid}",method=RequestMethod.GET)
	public String updatetype(@PathVariable("typeid") Integer typeid,Model model){
		Type type = service.selectByFtid(typeid);
		model.addAttribute("type", type);
		return "film/type/update";
	}
	//更新
	@RequestMapping(value="update/{typeid}",method=RequestMethod.PUT)
	public String updatetype(@PathVariable("typeid") Integer typeid,Type type){
		int result = service.updateByPrimaryKey(type);
		if(result == 1){
			return "redirect:/type/list";	
		}
		return  "redirect:error";
	}
	//删除单个影片类型信息。
	@RequestMapping(value="delete/{typeid}",method=RequestMethod.DELETE)
	public String deleteFun(@PathVariable("typeid") Integer typeid){
		int result = service.deleteByPrimaryKey(typeid);
		if(result==1){
			return "redirect:/type/list";	
		}
		return  "error";
	}
}
